package com.ddwl.common.exception;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;

/**
 * 类IllegalException.java的实现描述：非法请求异常
 *
 * @author zach
 * @Date :2020-06-02 16:52.
 */
public class IllegalException extends GlobalException{

    private static final long serialVersionUID = 6021390821349937519L;

    private static final String msg = "非法请求";

    public IllegalException(String message) {
        super(403,message);
    }
    public IllegalException() {
        super(403,msg);
    }

    public static void isTrue(boolean expression, String message) {
        if (expression) {
            throwException(message);
        }
    }

    public static void isTrue(boolean expression) {
        if (expression) {
            throwException(msg);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (!expression) {
            throwException(message);
        }
    }
    public static void isFalse(boolean expression) {
        if (!expression) {
            throwException(msg);
        }
    }

    public static void isNull(Object object, String message) {
        if (object == null) {
            throwException(message);
        }
    }

    public static void isNull(Object object, String message,Object... params) {
        if (object == null) {
            throwException(StrUtil.format(message,params));
        }
    }

    public static void isNotNull(Object object, String message) {
        if (object != null) {
            throwException(message);
        }
    }


    public static void isNull(Object obj) {
        isNull(obj, msg);
    }

    public static void isBlank(String text, String message) {
        if (!StringUtils.hasText(text)) {
            throwException(message);
        }
    }

    public static void isBlank(String text) {
        isBlank(text, msg);
    }

    private static void throwException(String msg){
        throw new IllegalException(msg);
    }
}
